export function merge(rangs) {
    rangs.sort((a, b) => a[0] - b[0])
    const merged = []
    for (const range of rangs) {
        if (merged.length === 0 || merged[merged.length - 1][1] < range[0]) {
            merged.push(range)
        } else {
            merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], range[1])
        }
    }
    return merged
}

export function total(rangs) {
    const merged = merge(rangs)
    let total = 0
    for (const [start, end] of merged) {
        if (start && end) {
            total += end - start
        }
    }
    return total
}
